package com.qingzhuge.member.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.qingzhuge.common.enums.ESexType;
import com.qingzhuge.common.validator.AddGroup;
import com.qingzhuge.common.validator.UpdateGroup;
import com.qingzhuge.entity.BaseModel;
import com.qingzhuge.interfaces.ISysUser;
import com.qingzhuge.member.enums.EnumThirdOauthType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * @author : zero.xiao
 * @description :
 * @date :2018/5/4 0004 下午 15:45
 * @modified :
 */
@ApiModel("A会员用户")
@Getter
@Setter
@Entity
@Table(name = "sys_member")
public class SysMember extends BaseModel implements ISysUser {
    private static final long serialVersionUID = 3350619221481362216L;

    /**
     * 用户名
     */
    @ApiModelProperty("用户名")
    @NotBlank(message = "用户名不能为空", groups = {AddGroup.class, UpdateGroup.class})
    private String username;

    /**
     * 密码
     */
    @ApiModelProperty("密码")
    @NotBlank(message = "密码不能为空", groups = AddGroup.class)
    @JSONField(serialize = false)
    private String password;

    /**
     * 昵称
     */
    @ApiModelProperty("昵称")
    private String nickname;
    /**
     * 邮箱
     */
    @ApiModelProperty("邮箱")
    @NotBlank(message = "邮箱不能为空", groups = {AddGroup.class, UpdateGroup.class})
    @Email(message = "邮箱格式不正确", groups = {AddGroup.class, UpdateGroup.class})
    private String email;

    /**
     * 手机号
     */
    @ApiModelProperty("手机号")
    private String mobile;
    @ApiModelProperty("电话号码")
    private String phone;
    @ApiModelProperty("性别")
    private ESexType sex;
    @ApiModelProperty("地址")
    private String address;
    /**
     * 头像
     */
    @ApiModelProperty("头像")
    @OneToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE})
    private SysMemberAvatar avatar;
    /**
     * 帐号注册类型，手机注册、微信、支付宝
     */
    @ApiModelProperty("注册类型")
    private EnumThirdOauthType registerType;
    @ApiModelProperty("登录类型")
    private EnumThirdOauthType loginType;
    /**
     * 当前登录ip
     */
    @ApiModelProperty("当前登录ip")
    private String currentLoginHost;
    /**
     * 上次登录ip
     */
    @ApiModelProperty("上次登录ip")
    private String lastLoginHost;
    /**
     * 上次登录时间
     */
    @ApiModelProperty("上次登录时间")
    private Date lastLoginTime;
    /**
     * 当前登录时间
     */
    @ApiModelProperty("当前登录时间")
    private Date currentLoginTime;
    /**
     * 登录次数
     */
    @ApiModelProperty("登录次数")
    private Integer loginCount;
    /**
     * 登录错误次数,用于用户登录次数过多时休眠用户登录状态
     */
    @ApiModelProperty("登录错误次数")
    private Integer loginErrorCount;

    public SysMember() {
    }

    public SysMember(Long id) {
        super(id);
    }

    public SysMember(Long id, String name, String nickname, String mobile, String phone, ESexType sex, SysMemberAvatar avatar) {
        super(name, id);
        this.nickname = nickname;
        this.mobile = mobile;
        this.phone = phone;
        this.sex = sex;
        this.avatar = avatar;
    }

    public String getAvatarUrl(){
        if (null != this.avatar){
            return this.avatar.getUrl();
        }else{
            return null;
        }
    }
}
